package com.xiaoma.myaudience.biz.adapter;

import java.util.List;
import java.util.Map;

import android.content.Context;
import android.text.TextUtils;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.ProgressBar;
import android.widget.RatingBar;
import android.widget.TextView;
import android.widget.LinearLayout.LayoutParams;

import com.xiaoma.myaudience.R;
import com.xiaoma.myaudience.biz.model.RankingListModel;
import com.xiaoma.myaudience.biz.view.LoadingImageView;
import com.xiaoma.myaudience.util.PicUtils;
import com.xiaoma.myaudience.util.model.ModelUtils;

/**
 * 首页面
 * @author jpren
 *
 */
public class HomeListAdapter extends BaseAdapter{

	private Context mContext;
	private List<Map<String, Object>> mList;	
	private LayoutInflater mInflater;
	private String mSizeStr;
	private String type;
	public String getType() {
		return type;
	}

	public void setType(String type) {
		this.type = type;
	}

	public HomeListAdapter(Context context, List<Map<String, Object>> list) {
		mContext = context;
		mList = list;
		mInflater = LayoutInflater.from(context);
		mSizeStr = PicUtils.composeSize((int) context.getResources().getDimension(
                R.dimen.list_item_img_width), (int) context.getResources().getDimension(
                R.dimen.list_item_img_height));
	}
	
	public HomeListAdapter(Context context, List<Map<String, Object>> list,String itype) {
		this.type = itype;
		mContext = context;
		mList = list;
		mInflater = LayoutInflater.from(context);
		mSizeStr = PicUtils.composeSize((int) context.getResources().getDimension(
                R.dimen.list_item_img_width), (int) context.getResources().getDimension(
                R.dimen.list_item_img_height));
	}
	
	@Override
	public int getCount() {
		return mList.size() + 1;
	}

	@Override
	public Object getItem(int position) {
		return mList.get(position);
	}

	@Override
	public long getItemId(int position) {
		return position;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		if (getCount() - 1 == position) {
			convertView = LayoutInflater.from(mContext).inflate(R.layout.more_horize_layout, null);
			Button mListMoreBtn = (Button) convertView.findViewById(R.id.list_more_btn);
		} else {
			convertView = mInflater.inflate(R.layout.home_recommend_item, null);
			ViewHolder holder = new ViewHolder();
			holder.img = (LoadingImageView) convertView.findViewById(R.id.img);
			holder.score = (TextView) convertView.findViewById(R.id.score);
			holder.ratingBar = (RatingBar) convertView.findViewById(R.id.rating_bar);
			holder.title = (TextView) convertView.findViewById(R.id.title);
			holder.zcr = (TextView) convertView.findViewById(R.id.zcr);
			holder.date = (TextView) convertView.findViewById(R.id.date);
	
			try {
				Map<String, Object> item = (Map<String, Object>) getItem(position);
				holder.img.setLoadingDrawable(ModelUtils.getStringValue(item, RankingListModel.PARAM_POSTER), mSizeStr);
				String score = ModelUtils.getStringValue(item, RankingListModel.PARAM_STARLEVEL);
				holder.score.setText(score);
				Double d = Math.ceil(Double.valueOf(score) / 2.0);
				float rating = TextUtils.isEmpty(score) ? 0 : Float.valueOf(score);
				holder.ratingBar.setRating(Float.valueOf(d.toString()));
				holder.title.setText(ModelUtils.getStringValue(item, RankingListModel.PARAM_TITLE));
				holder.zcr.setText("主持人:" + ModelUtils.getStringValue(item, RankingListModel.PARAM_ZCR));
				holder.date.setText("时间:" + ModelUtils.getStringValue(item, RankingListModel.PARAM_STARTTIME).replaceAll("\\s+", ""));
			} catch (Exception e) {
	
			}
		}
		
		return convertView;
	}

	static class ViewHolder {
        LoadingImageView img;
        TextView score;
        RatingBar ratingBar;
        TextView title;
        TextView zcr;
        TextView date;
    }

}